package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /*
    * 套餐的分页查询
    * */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 套餐的查询回显
    * */
    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
        Long dish_id = setmealVO.getId();
        List<SetmealDish> setmealDishesList = setmealMapper.selectSetmealDishById(dish_id);
        setmealVO.setSetmealDishes(setmealDishesList);
        return setmealVO;
    }

    /*
    * 新增套餐
    * */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 新增套餐的基本信息
        setmealMapper.save(setmeal);

        // 再新增套餐内的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            // 再新增套餐内的菜品
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }
    }

    /*
    * 修改套餐
    * */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 修改套餐的基本信息
        setmealMapper.update(setmeal);

        // 先删除套餐内的菜品
        setmealDishMapper.deleteById(setmeal.getId());

        // 再新增套餐内的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            // 再新增套餐内的菜品
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }
    }

    /*
    * 修改套餐状态
    * */
    @Override
    public void UpdateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /*
    * 根据ids批量删除套餐
    * */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        // 判断套餐是否在起售中
        for (Long id : ids){
            Integer status = setmealMapper.getStatusByIds(id);
            if (status == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }else{
                setmealMapper.deleteById(id);
            }
        }
    }

    /*
    * 根据分类id查询套餐
    * */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /*
    * 根据套餐id查询包含的菜品列表
    * */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealDishMapper.getDishItemById(id);
    }

}
